package com.finger.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.finger.common.exception.BizCodeEnume;
import com.finger.common.vo.MemberVo;
import com.finger.gulimall.member.exception.LoginException;
import com.finger.gulimall.member.exception.PhoneExistsExcepiton;
import com.finger.gulimall.member.vo.LoginVo;
import com.finger.gulimall.member.vo.RegistVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.finger.gulimall.member.entity.MemberEntity;
import com.finger.gulimall.member.service.MemberService;
import com.finger.common.utils.PageUtils;
import com.finger.common.utils.R;


/**
 * 会员
 *
 * @author finger
 * @email 909128695@qq.com
 * @date 2022-03-16 18:12:38
 */
@Log4j2
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//     @RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);


        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
        @RequestMapping("/info/{id}")
        // @RequiresPermissions("member:member:info")
        public R info(@PathVariable("id") Long id) {
            MemberEntity member = memberService.getById(id);

            return R.ok().put("mapper/member", member);
        }

        /**
         * 保存
         */
        @RequestMapping("/save")
        // @RequiresPermissions("member:member:save")
        public R save(@RequestBody MemberEntity member) {
            memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @CrossOrigin
    @GetMapping("/queryName/{name}")
    public R queryName(@PathVariable String name){
        Boolean exists = memberService.queryNameExists(name);

        return R.ok().put("exists",exists);
    }


    /**
     * 注册
     * @param registVo
     * @return
     */
    @PostMapping("/register")
    public R register(@RequestBody RegistVo registVo){

        try {
            memberService.register(registVo);
            return R.ok();
        }catch (LoginException e){
            e.printStackTrace();
            return R.error(15001,e.getMsg());
        }catch(PhoneExistsExcepiton e){
            e.printStackTrace();
            return  R.error(15002,"手机号已被占用");
        }catch (Exception e) {
            e.printStackTrace();
            return R.error("保存失败");
        }
    }

    /**
     * 用户登录
     * @param loginVo
     * @return
     */
    @PostMapping("login")
    public R login(@RequestBody LoginVo loginVo){

        try {
            // 调用登录接口实现登录功能
            MemberEntity login = memberService.login(loginVo);
            if(login!=null){
                MemberVo memberVo = new MemberVo();
                BeanUtils.copyProperties(login,memberVo);
                String jsonString = JSON.toJSONString(memberVo);
                // 登录成功将用户信息放入session域
                return R.ok(BizCodeEnume.MEMBER_LOGINACCT_SUCCESS).put("loginMember",jsonString);
            }else{
                // 校验错误
                return R.error(BizCodeEnume.MEMBER_LOGINACCT_FAILD);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(BizCodeEnume.UNKNOW_EXCEPTION);
        }

    }
}
